import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../view/Welcome.vue'
import User from '../view/User.vue'
import Rights from '../view/Rights.vue'
import Roles from '../view/Roles.vue'
import Cate from '../view/Cate.vue'
import Params from '../view/Params.vue'
import GoodsList from '../view/List.vue'
import Add from '../view/Add.vue'
import Order from '../view/Order.vue'
import Report from '../view/Report.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', name: 'login', component: Login },
  {
    path: '/home',
    name: 'home',
    component: Home,
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      name: 'welcome',
      component: Welcome
    },
    {
      path: '/users',
      name: 'users',
      component: User
    },
    {
      path: '/rights',
      name: 'rights',
      component: Rights
    },
    {
      path: '/roles',
      name: 'roles',
      component: Roles
    },
    {
      path: '/categories',
      name: 'categories',
      component: Cate
    },
    {
      path: '/params',
      name: 'params',
      component: Params
    },
    {
      path: '/goods',
      component: GoodsList
    },
    {
      path: '/goods/add',
      component: Add
    },
    {
      path: '/orders',
      component: Order
    },
    {
      path: '/reports',
      component: Report
    },
    ]
  }
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
router.beforeEach((to, form, next) => {
  //to 将要访问的路径
  //from 代表那个路径跳转过来
  //next 是一个函数，表示放行
  if (to.path === '/login') {
    return next();
  };
  //获取token
  let tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) {
    return next('/login');
  } else {
    next();
  }
});

export default router
